package edu.neusoft.yuejian_09.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.TrainSearchActivity;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.activity.TrainCainiaoActivity;
import edu.neusoft.yuejian_09.activity.TrainJinjieActivity;
import edu.neusoft.yuejian_09.activity.TrainRumenActivity;
import edu.neusoft.yuejian_09.adapter.TrainListAdapter;
import edu.neusoft.yuejian_09.bean.Train;
import edu.neusoft.yuejian_09.model.TrainModel;

public class TrainFragment extends Fragment implements RetrofitListener<List<Train>> {

    private RecyclerView recyclerView;
    private List mDataList;
    private View view=null;
    private RollPagerView rollPV = null;
    private ImageView rumen,cainiao,jinjie,img_search;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab02_train, container, false);
        initViews();
        initEvents();
        getTrainlist();
        return view;
    }

    private void initViews(){
        recyclerView= (RecyclerView)view.findViewById(R.id.rv_trainlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        rumen=(ImageView) view.findViewById(R.id.rumen_pic);
        cainiao=(ImageView) view.findViewById(R.id.cainiao_pic);
        jinjie=(ImageView) view.findViewById(R.id.jinjie_pic);
        img_search=(ImageView) view.findViewById(R.id.search);
    }

    private void initEvents(){
        img_search.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainSearchActivity.class);
                startActivity(intent);
            }
        });


        /****************轮播图*****************/
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPV.setHintView(new ColorPointHintView(getActivity(), getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPV.setAdapter(new RollPagerAdapter());

        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/

        /*入门跳转*/
        rumen.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainRumenActivity.class);
                startActivity(intent);
            }
        });
        cainiao.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainCainiaoActivity.class);
                startActivity(intent);
            }
        });
        jinjie.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                Intent intent = new Intent(TrainFragment.this.getActivity(), TrainJinjieActivity.class);
                startActivity(intent);
            }
        });



    }
    private int images[] = {
            R.drawable.lunbo1,
            R.drawable.lunbo2,
            R.drawable.lunbo3
    };

    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }


    void getTrainlist(){
        TrainModel activityModel=new TrainModel();
        activityModel.astivitylist(this);
    }

    @Override
    public void onSuccess(List<Train> data,int flag ){

        //System.out.printf("data");
        recyclerView.setAdapter(new TrainListAdapter(TrainFragment.this.getActivity(),data));
    }

    @Override
    public void onFail(){ Toast.makeText(getActivity(), "网络发生错误", Toast.LENGTH_SHORT).show();}

}